package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"github.com/garyburd/redigo/redis"
	"os"
	"runtime"
	"strconv"
	"time"
)

var resource_path string
var num = 0

func main() {
	//解析变量
	var cacheKey string
	flag.StringVar(&cacheKey, "cacheKey", "", "redis key")
	flag.Parse()
	flag.Usage()
	fmt.Printf("cacheKey=%s", cacheKey)

	//连接Redis
	conn,err := redis.Dial("tcp","127.0.0.1:6379",
		redis.DialDatabase(1),//dialOption参数可以配置选择数据库、连接密码、心跳检测等等
		redis.DialPassword("lxj666"))
	if err != nil {
		fmt.Println("connect redis err:",err)
		return
	}
	defer conn.Close()
	//设置值
	_,err = conn.Do("SET","name",cacheKey)
	if err != nil {
		fmt.Println("error:",err)
	}
	name,err := redis.String(conn.Do("GET","name"))
	if err != nil {
		fmt.Println("error:",err)
	}else {
		fmt.Printf("My name is:%s \n",name)
	}
	//设置过期时间
	_,err = conn.Do("expire","name",30) //10秒过期
	if err != nil {
		fmt.Println("error:",err)
		return
	}

	//连接oss 签名
	client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "osskey", "ossSignature")
	if err != nil {
		fmt.Println("1Error:", err)
		os.Exit(-1)
	}

	// 获取存储空间。
	bucket, err := client.Bucket("wtbl-dev")
	if err != nil {
		fmt.Println("2Error:", err)
		os.Exit(-1)
	}

	//文件数据json
	var m map[string]interface{}
	json_str := "{\"data\":[{\"name\":\"19--创意评审组测试-7.5测试专家2-1.pdf\",\"image_url\":\"test_zj\\/file\\/20200709\\/5f06cede95e49.pdf\"},{\"name\":\"100-7.9测试赛事评审须知-评审须知创意组-7.5测试专家2-1.jpg\",\"image_url\":\"test_zj\\/file\\/20200729\\/5f20df809e493.jpg\"},{\"name\":\"101-7.9测试赛事评审须知-评审须知创意组-7.5测试专家2-2.jpg\",\"image_url\":\"test_zj\\/file\\/20200729\\/5f20df8639fbd.jpg\"},{\"name\":\"102-7.9测试赛事评审须知-评审须知创意组-7.5测试专家2-3.png\",\"image_url\":\"test_zj\\/file\\/20200729\\/5f20df8ab622a.png\"},{\"name\":\"103-7.9测试赛事评审须知-评审须知创意组-7.5测试专家2-4.png\",\"image_url\":\"test_zj\\/file\\/20200729\\/5f20df8f9b180.png\"},{\"name\":\"104-7.9测试赛事评审须知-评审须知创意组-7.5测试专家2-5.png\",\"image_url\":\"test_zj\\/file\\/20200729\\/5f20df94a6e5e.png\"},{\"name\":\"105-7.9测试赛事评审须知-评审须知创意组-7.5测试专家2-6.jpg\",\"image_url\":\"test_zj\\/file\\/20200729\\/5f20df9b4a7ed.jpg\"},{\"name\":\"106-7.9测试赛事评审须知-评审须知创意组-7.5测试专家2-7.jpg\",\"image_url\":\"test_zj\\/file\\/20200729\\/5f20dfa0be6a2.jpg\"},{\"name\":\"107-7.9测试赛事评审须知-评审须知创意组-7.5测试专家2-8.png\",\"image_url\":\"test_zj\\/file\\/20200729\\/5f20dfa5899cf.png\"},{\"name\":\"108-7.9测试赛事评审须知-评审须知创意组-7.5测试专家2-9.png\",\"image_url\":\"test_zj\\/file\\/20200729\\/5f20dfaacc439.png\"},{\"name\":\"109-7.9测试赛事评审须知-评审须知创意组-7.5测试专家2-10.jpg\",\"image_url\":\"test_zj\\/file\\/20200729\\/5f20dfaf659ac.jpg\"},{\"name\":\"21-7.9测试赛事评审须知-评审须知创意组-测试专家2-1.xlsx\",\"image_url\":\"test_zj\\/file\\/20200712\\/5f0abb0239338.xlsx\"},{\"name\":\"22-技术支持专用测试-技术支持-周-1.jpg\",\"image_url\":\"test_zj\\/file\\/20200720\\/5f15354c4ef47.jpg\"},{\"name\":\"23-赛事名称-评审组名称-郝云龙-1.jpeg\",\"image_url\":\"test_zj\\/img\\/20200724\\/5f1a57c548d9c.jpeg\"},{\"name\":\"25-7.5测试赛事--测试专家导入-1.jpg\",\"image_url\":\"test_zj\\/file\\/20200724\\/5f1a58ca5cbb2.jpg\"},{\"name\":\"64-7-12测试赛事-测试创意组-7.5测试专家2-1.jpg\",\"image_url\":\"test_zj\\/file\\/20200728\\/5f1f94e9b5b91.jpg\"},{\"name\":\"50-评委组测试赛事-123123-7.5测试专家2-1.jpg\",\"image_url\":\"test_zj\\/file\\/20200727\\/5f1ea379a5429.jpg\"},{\"name\":\"51-评委组测试赛事-123123-7.5测试专家2-2.jpg\",\"image_url\":\"test_zj\\/file\\/20200727\\/5f1ea37cf205f.jpg\"},{\"name\":\"81-赛事名称-评审组名称-专家2-1.jpg\",\"image_url\":\"test_zj\\/file\\/20200728\\/5f1fb0826ee58.jpg\"},{\"name\":\"82-赛事名称-评审组名称-专家2-2.jpg\",\"image_url\":\"test_zj\\/file\\/20200728\\/5f1fb0860c25c.jpg\"},{\"name\":\"83-赛事名称-评审组名称-专家2-3.jpeg\",\"image_url\":\"test_zj\\/file\\/20200728\\/5f1fb08a37fba.jpeg\"},{\"name\":\"84-赛事名称-评审组名称-专家2-4.jpg\",\"image_url\":\"test_zj\\/file\\/20200728\\/5f1fb08e8540e.jpg\"},{\"name\":\"85-赛事名称-评审组名称-专家2-5.jpeg\",\"image_url\":\"test_zj\\/file\\/20200728\\/5f1fb09237212.jpeg\"},{\"name\":\"86-赛事名称-评审组名称-专家2-6.jpeg\",\"image_url\":\"test_zj\\/file\\/20200728\\/5f1fb0a08800d.jpeg\"},{\"name\":\"87-赛事名称-评审组名称-专家2-7.jpeg\",\"image_url\":\"test_zj\\/file\\/20200728\\/5f1fb0a5098c6.jpeg\"},{\"name\":\"88-赛事名称-评审组名称-专家2-8.jpg\",\"image_url\":\"test_zj\\/file\\/20200728\\/5f1fb0aa25271.jpg\"},{\"name\":\"89-赛事名称-评审组名称-专家2-9.jpeg\",\"image_url\":\"test_zj\\/file\\/20200728\\/5f1fb0ae526aa.jpeg\"},{\"name\":\"90-赛事名称-评审组名称-专家2-10.jpeg\",\"image_url\":\"test_zj\\/file\\/20200728\\/5f1fb0b24c228.jpeg\"},{\"name\":\"111-“建行杯”     第六届中国国际互联网+大学生创新创业大赛陕西赛区冠军争夺赛（青红）-评审组（2）-郝云龙-1.png\",\"image_url\":\"test_zj\\/img\\/20201026\\/5f969cc5d1c51.png\"},{\"name\":\"112-7.31测试赛事新建-7.31测试创意组-测试专家导入-1.xlsx\",\"image_url\":\"test_zj\\/file\\/20200803\\/5f27c65c8e373.xlsx\"},{\"name\":\"121-帅气洋测试赛事-测试A,测试B-专家2-1.jpg\",\"image_url\":\"test_zj\\/file\\/20200804\\/5f2940b845825.jpg\"},{\"name\":\"122-帅气洋测试赛事-测试A,测试B-专家2-2.jpg\",\"image_url\":\"test_zj\\/file\\/20200804\\/5f2940bb07f87.jpg\"},{\"name\":\"123-帅气洋测试赛事-测试A,测试B-专家2-3.jpg\",\"image_url\":\"test_zj\\/file\\/20200804\\/5f2940bd9ec8f.jpg\"},{\"name\":\"124-帅气洋测试赛事-测试A,测试B-专家2-4.jpg\",\"image_url\":\"test_zj\\/file\\/20200804\\/5f2940c017479.jpg\"},{\"name\":\"125-帅气洋测试赛事-测试A,测试B-专家2-5.jpg\",\"image_url\":\"test_zj\\/file\\/20200804\\/5f2940c2819b4.jpg\"},{\"name\":\"126-帅气洋测试赛事-测试A,测试B-专家2-6.jpg\",\"image_url\":\"test_zj\\/file\\/20200804\\/5f2940c524ad0.jpg\"},{\"name\":\"127-帅气洋测试赛事-测试A,测试B-专家2-7.jpg\",\"image_url\":\"test_zj\\/file\\/20200804\\/5f2940c89ff27.jpg\"},{\"name\":\"128-帅气洋测试赛事-测试A,测试B-专家2-8.jpg\",\"image_url\":\"test_zj\\/file\\/20200804\\/5f2940cb853ae.jpg\"},{\"name\":\"129-帅气洋测试赛事-测试A,测试B-专家2-9.jpg\",\"image_url\":\"test_zj\\/file\\/20200804\\/5f2940ce1909c.jpg\"},{\"name\":\"132-0011-0011-郝云龙-1.jpeg\",\"image_url\":\"test_zj\\/img\\/20200807\\/5f2cfaff6b628.jpeg\"},{\"name\":\"133-赛事名称-评审组名称（5）-郝云龙-1.png\",\"image_url\":\"test_zj\\/img\\/20200907\\/5f558a8282033.png\"},{\"name\":\"136-8.6测试赛事评审方式-创意1组-7.4测试专家-1.png\",\"image_url\":\"test_zj\\/img\\/20200903\\/5f509824acfdb.png\"},{\"name\":\"137-8.6测试赛事评审方式-创意1组-7.4测试专家-2.jpeg\",\"image_url\":\"test_zj\\/file\\/20200814\\/5f362a7b4466f.jpeg\"},{\"name\":\"138-8.6测试赛事评审方式-创意1组-7.4测试专家-3.jpg\",\"image_url\":\"test_zj\\/file\\/20200814\\/5f362a80e21b9.jpg\"},{\"name\":\"139-赛事名称（2）-维度打分-郝云龙-1.png\",\"image_url\":\"test_zj\\/img\\/20200909\\/5f583c20103ca.png\"},{\"name\":\"141-评审抽签-测试组1-7.5测试专家-1.jpeg\",\"image_url\":\"test_zj\\/img\\/20200821\\/5f3f7caa7dd58.jpeg\"},{\"name\":\"142-7.5测试赛事--7.4测试专家-1.png\",\"image_url\":\"test_zj\\/img\\/20200903\\/5f504b209d115.png\"},{\"name\":\"143-测试阿寒评委组-阿寒-郝云龙-1.png\",\"image_url\":\"test_zj\\/img\\/20201026\\/5f96352a8387c.png\"},{\"name\":\"144-赛事名称（2）-评审组（2）-123123-1.png\",\"image_url\":\"test_zj\\/img\\/20200902\\/5f4f4760deab5.png\"},{\"name\":\"145-td测试赛事“建行杯” 第六届中国国际互联网+大学生创新创业大赛陕西赛区冠军争夺赛-无情-童得专家-1.png\",\"image_url\":\"test_zj\\/img\\/20201025\\/5f952511e8d37.png\"},{\"name\":\"146-7.5测试赛事--张建军-1.png\",\"image_url\":\"test_zj\\/img\\/20200904\\/5f51d77bbaaab.png\"},{\"name\":\"147-测试阿寒评委组-阿寒-张建军-1.png\",\"image_url\":\"test_zj\\/img\\/20200904\\/5f51d7bd31959.png\"},{\"name\":\"148-8.6测试赛事评审方式-创意1组-123123-1.png\",\"image_url\":\"test_zj\\/img\\/20200907\\/5f55dd8d7ef57.png\"},{\"name\":\"150-9.8总分赛事-9.9总分评审组-张建军-1.png\",\"image_url\":\"test_zj\\/img\\/20200909\\/5f587bb081cb5.png\"},{\"name\":\"151-9.8总分赛事-9.9总分评审组-童得专家-1.png\",\"image_url\":\"test_zj\\/img\\/20200909\\/5f587bc45df1b.png\"},{\"name\":\"155-“建行杯” 第六届浙江互联网+大学生创新创业大赛-高教主创意1组-评委3号-1.png\",\"image_url\":\"test_zj\\/img\\/20210107\\/5ff669c28658a.png\"},{\"name\":\"163-test-混合测试混合组测试-评委10号-1.png\",\"image_url\":\"test_zj\\/img\\/20210120\\/6007f6d31775e.png\"},{\"name\":\"164-test-混合测试混合组测试-评委8号-1.png\",\"image_url\":\"test_zj\\/img\\/20210120\\/6007fa4461cd6.png\"},{\"name\":\"165-test-混合测试混合组测试2-评委8号-1.png\",\"image_url\":\"test_zj\\/img\\/20210120\\/6007fa5a9351a.png\"},{\"name\":\"166-混合组别打分赛事-混合组别打分-唐传专家-1.png\",\"image_url\":\"test_zj\\/img\\/20210122\\/600a4bf2eaedd.png\"},{\"name\":\"167-“建行杯” 第六届浙江互联网+大学生创新创业大赛-高教主创意1组-评委1号-1.png\",\"image_url\":\"test_zj\\/img\\/20210121\\/6008ee9b79466.png\"},{\"name\":\"169-混合组别打分赛事-混合组别打分3-唐传专家-1.png\",\"image_url\":\"test_zj\\/img\\/20210122\\/600a88895550c.png\"},{\"name\":\"170-2021.4Pad打分-非洲是你家-密码复杂度-1.png\",\"image_url\":\"test_zj\\/img\\/20210423\\/60826de6483df.png\"},{\"name\":\"171-2021.4Pad打分-非洲是你家-童得专家-1.png\",\"image_url\":\"test_zj\\/img\\/20210423\\/60827c2d8351f.png\"},{\"name\":\"172-10.12赛事-10.12评审组-童得专家-1.jpg\",\"image_url\":\"test_zj\\/file\\/20210429\\/608a8363b2eac.jpg\"},{\"name\":\"175-超过10条测试-评审11组评审11组评审11组评审11组-车石吉专家-1.png\",\"image_url\":\"test_zj\\/file\\/20210615\\/60c857c6c6190.png\"},{\"name\":\"177-测试阿寒评委组-阿寒-产桂兰-1.png\",\"image_url\":\"test_zj\\/img\\/20210623\\/60d2e23a4bf8e.png\"},{\"name\":\"178-测试阿寒评委组-阿寒-1212-1.png\",\"image_url\":\"test_zj\\/img\\/20210623\\/60d2fb641cc4d.png\"},{\"name\":\"179-测试阿寒评委组-阿寒-123123-1.png\",\"image_url\":\"test_zj\\/img\\/20210626\\/60d6f49000183.png\"},{\"name\":\"182-123456-高教1-yf-1.png\",\"image_url\":\"test_zj\\/file\\/20210628\\/60d985489cfa0.png\"},{\"name\":\"183-测试阿寒评委组-阿寒-测试-1.png\",\"image_url\":\"test_zj\\/file\\/20210629\\/60dacfe884331.png\"},{\"name\":\"186-2021.5赛事PC--闫振銮-1.jpg\",\"image_url\":\"test_zj\\/file\\/20210629\\/60dad787d6712.jpg\"},{\"name\":\"187-1111tyk-评审组111,评审组tyk222-闫振銮-1.png\",\"image_url\":\"test_zj\\/img\\/20210629\\/60dadc9adc957.png\"},{\"name\":\"188-1111tyk-评审组111,评审组tyk222-闫振銮-2.jpg\",\"image_url\":\"test_zj\\/file\\/20210629\\/60dadc51c62eb.jpg\"},{\"name\":\"189-“建行杯” 第六届浙江互联网+大学生创新创业大赛-高教主创意1组-产桂兰-1.png\",\"image_url\":\"test_zj\\/img\\/20210629\\/60dae7b5bbd20.png\"},{\"name\":\"191-2021.4Pad打分-2021.4Pad打分-混合组-车石吉专家-1.png\",\"image_url\":\"test_zj\\/img\\/20210708\\/60e6957d2677b.png\"}]}"
	err = json.Unmarshal([]byte(json_str), &m)

	if err != nil {
		fmt.Println(err)
		os.Exit(2)
	}

	start_time := time.Now().Unix()
	//开启线程数
	thread_num := 4
	//使用多核
	runtime.GOMAXPROCS(runtime.NumCPU())
	ch := make(chan bool, thread_num)
	if project_data, ok := m["data"].([]interface{}); ok {
		length := len(project_data)
		//fmt.Println(m["data"].([]interface{}))
		fmt.Println("总任务数：" + fmt.Sprintf("%d", length))

		//每组用户数
		group_size := length / thread_num
		//不够多协程分配则改为单协程
		if group_size == 0 {
			thread_num = 1
		}
		var pd []interface{}
		for i := 0; i < thread_num; i++ {
			//分割每组数据
			start_index := i * int(group_size)
			end_index := (i + 1) * int(group_size)
			if i == thread_num-1 {
				pd = project_data[start_index:]
			} else {
				pd = project_data[start_index:end_index]
			}
			//fmt.Println(len(pd))
			//执行协程
			go GetObjectToFile(bucket, pd, ch)
		}
	} else {
		fmt.Println("project_data error")
		os.Exit(4)
	}
	for i := 0; i < thread_num; i++ {
		<-ch
	}
	use_time := time.Now().Unix() - start_time
	fmt.Println("下载结束,用时" + strconv.Itoa(int(use_time)) + "秒！！！10秒后自动关闭！！！")
	close(ch)
	time.Sleep(time.Duration(10) * time.Second)
	fmt.Println("true")

}

func GetObjectToFile(bucket *oss.Bucket, project_data []interface{}, ch chan bool) {
	for _, fileItem := range project_data {
		fileMap := fileItem.(map[string]interface{})
		// 下载文件到本地文件。
		err := bucket.GetObjectToFile(fileMap["image_url"].(string), "./upload/"+fileMap["name"].(string))
		if err != nil {
			fmt.Println("Error:", err)
			os.Exit(-1)
		}
		num++
		fmt.Println("已完成：" + fmt.Sprintf("%d", num) + "    文件名：" + fileMap["name"].(string))
	}
	ch <- true
}
